// /* In this workshop, you will be introduced to image processing in Google
// Earth Engine.

// Google Earth Engine is a cloud-based platform that enables large-scale
// processing of imagery to detect changes, map trends, and quantify
// differences on the Earth’s surface.

// This workshop will focus on mapping changes in surface area levels over
// Shasta Lake this past year using 10-meter resolution Sentinel2 imagery.


// TIP: Use "command-/" to uncomment chunks of code for the workshop
// */

// /* SECTION 1: Create and display a Geometry over Shasta Lake ////////////////*
// /* Define the region of interest (ROI) - Shasta Lake*/
// var shasta_lake = ee.Geometry.Polygon(
//         [[[-122.48691050204125,40.7049384269217],
//           [-122.11474863680688,40.7049384269217],
//           [-122.11474863680688,40.898294348395034],
//           [-122.48691050204125,40.898294348395034],
//           [-122.48691050204125,40.7049384269217]]]);

// /* Add Shasta Lake to the map */
// Map.addLayer(shasta_lake, {width: 2, fillOpacity: 0}, 'Shasta Lake Geometry');
// Map.centerObject(shasta_lake, 10);





// /*SECTION 2: Load Sentinel-2 Imagery ///////////////////////////////////////*

// /* We'll be pulling imagery from Sentinel-2
// View the asset in the catalog at the following URL:
// https://developers.google.com/earth-engine/datasets/catalog/COPERNICUS_S2#description
// */

// /* Load Sentinel-2 imagery (RGB bands) and filter by date*/
// var sentinel2 = ee.ImageCollection('COPERNICUS/S2')
//   .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
//   .filterBounds(shasta_lake);
// print(sentinel2.first());

// /* Plot RGB Sentinel-2 globally for 2022*/
// var rgbVis = {
//   min: 0,
//   max: 4000,
//   bands: ['B4', 'B3', 'B2'],
// };
// var s2_filtered = sentinel2.filterDate('2022-06-01', '2022-07-01');
// //Map.addLayer(s2_filtered.median(), rgbVis, 'Sentinel2 June 2022');

// /* ACTION REQUIRED: When done reviewing S2 globally, comment out the line
// above to reduce lag in future displays*/





// /* SECTION 3: Filter Dates and Plot Monthly Medians /////////////////////////*
// /* According to NOAA, Shasta Lake experienced heavy rainfall in December
// 2022 and January of 2023 this past season.  Let's check on the water levels
// before and after this event.*/

// var Nov_2022 = sentinel2.filterDate('2022-11-01', '2022-12-01')
// var Feb_2023 = sentinel2.filterDate('2023-02-01', '2023-03-01')

// Map.addLayer(Nov_2022.median().clip(shasta_lake), rgbVis, 'RGB Nov 2022');
// Map.addLayer(Feb_2023.median().clip(shasta_lake), rgbVis, 'RGB Feb 2023');





// /* SECTION 4: Map NDWI //////////////////////////////////////////////////////*

// /* Can we use a spectral index to identify water pixels?
// Calculate Normalized Difference Water Index (NDWI)*/
// var calculateNDWI = function(image) {
//   var ndwi = image.normalizedDifference(['B3', 'B8']).rename('NDWI');
//   return image.addBands(ndwi);
// };

// /* Apply the NDWI function to the collection, when printing note
// the new NDWI band */
// var Nov_2022 = Nov_2022.map(calculateNDWI);
// var Feb_2023 = Feb_2023.map(calculateNDWI);
// print(Nov_2022.first())

// /* Define visualization parameters for NDWI */
// var ndwiVis = {
//     min: -1,
//     max: 1,
//     palette: ['white', 'darkblue']
// };

// /* Save NDWI as variables */
// var Nov_ndwi = Nov_2022.select("NDWI").median().clip(shasta_lake);
// var Feb_ndwi = Feb_2023.select("NDWI").median().clip(shasta_lake);

// Map.addLayer(Nov_ndwi, ndwiVis, 'NDWI Nov 2022');
// Map.addLayer(Feb_ndwi, ndwiVis, 'NDWI Feb 2023');





// /* SECTION 5:  Classify Water Pixels ///////////////////////////////////////*

// /* Let's establish a threshold above which values are "water" */
// print(ui.Chart.image.histogram({image: Nov_ndwi, scale: 100}));
// print(ui.Chart.image.histogram({image: Feb_ndwi, scale: 100}));

// /* The histogram displays two major distributions, the higher of which
// is composed of values over the reservoir*/
// var threshold = 0.15

// /* Classify water in both images by creating binary masks for each image
// based on the threshold*/
// var Nov_water = Nov_ndwi.gt(threshold); // Greater than threshold -> True, otherwise False
// var Feb_water = Feb_ndwi.gt(threshold);

// Map.addLayer(Nov_water, {}, 'water Nov 2022');
// Map.addLayer(Feb_water, {}, 'water Feb 2023');






// /* SECTION 6: Map Pixels that are Newly Aqueous ////////////////////////////*

// /*Compute the difference between the two masks*/
// var maskDifference = Feb_water.subtract(Nov_water);

// /*Display the masks */
// Map.addLayer(maskDifference, {min: -1, max: 1, palette: ['grey', 'white']}, 'Mask Difference');

// /*Calculate the total area of the maskDifference*/
// var area = maskDifference.reduceRegion({
//   reducer: ee.Reducer.sum(),
//   geometry: shasta_lake,
//   scale: 10, // meters
// });
// print("Total area in m2 of surface area change: ", area);


// /* Workshop Complete */
